package com.flex.core.validate.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.flex.core.validate.AbstractValidator;

/**
 * 正则表达式验证器，只能对字符串类型的属性进行验证
 * 
 * @author radio
 * 
 */
public class RegexValidator extends AbstractValidator {

	/**
	 * 是否使用内置的正则表达式
	 */
	private boolean caseSensitive = true;

	/**
	 * 内置正则表达式，给子类扩展用
	 */
	private String expression = "";

	/**
	 * 实现父类的验证方法，使用正则表达式对属性值进行验证
	 */
	protected String doValidate() {
		String msg = "";
		Pattern pattern;
		if (isCaseSensitive()) {
			pattern = Pattern.compile(getValidation().regex());
		} else {
			pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		}
		Matcher matcher = pattern.matcher(getFiledValue().toString().trim());
		if (!matcher.matches()) {
			msg = getValidation().message();
		}
		return msg;
	}

	protected boolean isCaseSensitive() {
		return caseSensitive;
	}

	public void setCaseSensitive(boolean caseSensitive) {
		this.caseSensitive = caseSensitive;
	}

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}

	public String formateValue() {
		return (String) getFiledValue();
	}

}
